/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.its.dsw2.view;

import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.its.dsw2.biblioteca.bl.CategoriaBlLocal;
import mx.its.dsw2.biblioteca.modelo.Categoria;
import mx.its.dsw2.biblioteca.modelo.Usuario;

/**
 *
 * @author lesly
 */
@ManagedBean
@SessionScoped
public class CategoryBean{
    
    @EJB
    private CategoriaBlLocal categoriaBl;
    
    private List<Categoria> lista;
    private Categoria category;
     private String accion;
    
    /**
     * Creates a new instance of CategoryBean
     */
    
    
    
    public CategoryBean() {
    }

    public List<Categoria> getLista() {
        lista=categoriaBl.getAll();
        return lista;
    }
   
    public void setLista(List<Categoria> lista) {
        this.lista = lista;
    }

    public Categoria getCategory() {
        return category;
    }

    public void setCategory(Categoria category) {
        this.category = category;
    }
    
    public void prepararNuevo(ActionEvent event){
        accion="Nuevo";
    category=new Categoria();
    }
    
    public String procesarAccion(){
    if(accion.equalsIgnoreCase("Nuevo")){
    categoriaBl.register(category);
    }else{
    if(accion.equalsIgnoreCase("Editar")){
    categoriaBl.modify(category);
    }else{
    if(accion.equalsIgnoreCase("Eliminar")){
    categoriaBl.delete(category);
    }
    }
    }
    accion=" ";
    return "CategoryList";
    }
      public void prepararEditar(ActionEvent evt) {
       accion="Editar";
      System.out.println(evt.getComponent().getClientId());        
        int index=Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        category=lista.get(index);
    } 
   public void prepararEliminar(ActionEvent evt){
   accion="Eliminar";
  // System.out.println(evt.getComponent().getClientId());
    int index=Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        category=lista.get(index);
   
   }
   
   public String procesarRegresar(){
        if(accion.equalsIgnoreCase("Nuevo")|| accion.equalsIgnoreCase("Editar")){
            return "CategoryCreateEdit";
        }else{
            return "CategoryList";
        }
    }
}